<?php
set_time_limit(120);

ob_start();
$isPDF = true;
include('spider_articles.php');
$html = ob_get_clean();

// Remove Digg it buttons
while (eregi('<iframe src="([^"]+)" align="right" frameborder="0" height="82" scrolling="no" width="55"></iframe>', $html, $regs)) {
	$html = str_replace($regs[0], /*getDigThisHTML($regs[1])*/ '', $html);
}

if (empty($config['prince'])) {
	die("You need to define the path to Prince XML executable in your configuration before you can use this.");
}

$filename = 'GobbleRSS-'.date('YmdHi').'.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="'.$filename.'"');

require_once("prince/prince.php");
$prince = new Prince($config['prince']);
$prince->setHTML(true);
$prince->setLog($config['log_file']);

ob_start();
$prince->convert3($html);
$pdf = ob_get_clean();

file_put_contents('cache/'.$filename, $pdf);
print($pdf);
exec('find cache/ -not -mtime -7 -name *.pdf -exec rm -f {} \;');

function getDigThisHTML($url) {
	$url = explode('u=', $url);
	$url = $url[0].'u='.urlencode($url[1]);
	exec('curl "'.$url.'"', $html);
	$html = implode('', $html);
	$html = str_ireplace(array('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"','  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">','<html xmlns="http://www.w3.org/1999/xhtml">','<head>','</head>','<body style="background:#fff">','</body>','</html>'), '', $html);
	$html = eregi_replace('<title>[^<]+</title>', '', $html);
	$html = eregi_replace('<meta[^>]+/>', '', $html);
	return $html;
}
